#include "app_common.h"
#include "sys/time.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
long long app_common_getCurrentTime()
{
    struct timeval tv;
    
    gettimeofday(&tv,NULL);

    return (long long)tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

char *app_common_charsToHexStr(const char *chars, int len)
{
    //分配十六进制字符串内存
    char *hexStr = (char *)malloc(len * 2 + 1);
    if (hexStr == NULL) {
        return NULL;
    }
    //将字符数组转换为十六进制字符串
    for (int i = 0; i < len; i++) {
        sprintf(hexStr + i * 2, "%02X", chars[i]);
    }
    //添加字符串结束符
    hexStr[len * 2] = '\0';

    return hexStr;
}

char *app_common_hexStrToChars(const char *hexStr, int *charsLen)
{
    //计算十六进制字符串长度
    int hexStrLen = strlen(hexStr);
    //计算字符数组长度
    *charsLen = hexStrLen / 2;
    //分配字符数组内存
    char *chars = (char *)malloc(*charsLen);
    if (chars == NULL) {
        return NULL;
    }
    //将十六进制字符串转换为字符数组
    for (int i = 0; i < *charsLen; i++) {
        sscanf(hexStr + i * 2, "%2hhx", chars + i);
    }

    return chars;
}
